<?php
namespace Admin\Controller;
use Think\Controller;
class LinkController extends Controller {

	public function index(){
		$link = M("Link");
		$count=$link->count();
		$Page =new \Think\Page($count,5);
		$links =$link->field('id,linkname,links')->order('id asc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('page',$Page->show());
		$this->assign("links",$links);
		$this->display();
	}

	public function add(){
		if($_POST['sub']){
			if(!(empty($_POST['linkname']))&&!(empty($_POST['links']))){
				// var_dump(111);
				$link = M("link");
				$_POST['linkname']=$_POST['linkname'];
				$_POST['links']=$_POST['links'];
				if($link->create()){
					if($link->add()){
						$this->redirect('Link/index');
					}else{
						$this->error("添加失败！");
					}
				}
			}
		}
		$this->display();
	}

	public function mod(){
		$link = M("link");
		$id=$_GET['id'];
		$data= $link->find($id);
		if($_POST['sub']){
			if(!(empty($_POST['linkname']))&&!(empty($_POST['links']))){
				if($_POST['linkname'] && $_POST['links']){
					$_POST['linkname']=$_POST['linkname'];
					$_POST['links']=$_POST['links'];
				}else{
					unset($_POST['linkname']);
					unset($_POST['links']);
				}
				if($link->create()){
					if($link->save()){
						$this->redirect("Link/index");
					}else{
						$this->error("修改失败！");
					}
				}
			}
		}
		$this->assign('link',$data);
		$this->display();
	}

	public function del(){
		$link =M("link");
		$id=I('id');
		$map['id']=array("IN",$id);
		if($link->where($map)->delete($id)){
			$this->redirect("Link/index");
		}else{
			$this->error("删除失败！");
		}
	}
}